package usually;
//240. 搜索二维矩阵 II
public class Num240_searchMatrix {
    /**
     * 二分查找
     */
    public boolean searchMatrix(int[][] matrix, int target) {
        //遍历每
        for (int []row : matrix){
            if (search(row,target)>=0){
                return true;
            }

        }
        return false;

    }

    public int search(int[] nums, int target) {
        int low = 0 ;
        int high = nums.length - 1;
        while (low <= high){
            int mid = low + (high - low) / 2 ;
            if (nums[mid] > target){
                high = mid - 1;
            }else if (nums[mid] < target){
                low = mid + 1 ;
            }else {
                return mid;
            }
        }
        return -1;
    }
}

    /**
     * 普通查找
     */
//    public boolean searchMatrix(int[][] matrix, int target) {
//        int row = 0;
//        int col = matrix[0].length - 1;
//        int rowMax = matrix.length - 1;
//        while (row <= rowMax && col >= 0){
//            if (matrix[row][col] > target){
//                col--;
//
//            }else if (matrix[row][col] < target){
//                row++;
//
//            }else {
//                return true;
//            }
//
//        }
//        return false;

    //}

